package com.vcredit.creditcat.time.creditassure;

import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.creditcat.manager.CreditAssureTimaManager;
import com.vcredit.creditcat.model.CreditAssure;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.framework.quartz.QuartzJob;

/**
 * 结果获取
 */
@QuartzJob(name = "creditAssureResultTime", cron = "0 0/10 * * * ?")
public class CreditAssureResultTime  extends QuartzJobBean{

	private Logger logger = LoggerFactory.getLogger(CreditAssureApplyTime.class);

	@Autowired
	private  CreditAssureTimaManager creditAssureTimaManager;

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		logger.info("机构版本征信认证结果定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
	
		List<CreditAssure> caList = creditAssureTimaManager.findToGetReport();
		if (caList == null || caList.isEmpty()){
			return;
		}
		for (CreditAssure ca : caList) {
			threadPoolTaskExecutor.execute(new CrawleThread(ca));
		}
	}

	class CrawleThread implements Runnable {
		private CreditAssure obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(CreditAssure obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				creditAssureTimaManager.toGetResult(obj);
			} catch (Exception e) {
				logger.error("Exception occured", e);
			}
		}
	}
}
